package com.jms.fu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jms.fu.exception.DAOException;
import com.jms.fu.model.bean.App;
import com.jms.fu.model.bo.AppBO;
import com.jms.fu.model.bo.AppDownBO;

public class GetUserDownloadAppAction extends Action{

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		PrintWriter out = null;
		String userlogin = (String) request.getSession().getAttribute("username");
		String role = (String)request.getSession().getAttribute("role");
		if(userlogin==null||role==null){
			return null;
		}
		String username = (String) request.getParameter("usernameCheck");
		if(username==null){
			return null;
		}
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		AppBO appbo = new AppBO();
		ArrayList<App> list = new ArrayList<App>();
		
		try {
			list = appbo.getAppDownByUser(username);
		} catch (DAOException e) {
			e.printStackTrace();
			return null;
		}
		String applistHTML ="";
		AppDownBO appdown = new AppDownBO();
		
		
		try{
		for(App temp:list){
			boolean check;
			if(username!=null){
			check = ((username.equals(temp.getDevId())) || (role
					.equals("Administrator")));
			}else{
				check = false;
			}
			ArrayList<String> avatar = appbo.getNumberOfFile(getServlet().getServletContext().getRealPath("/")+temp.getLink()+"/Avatar");
			String avatarLink;
			if(avatar.size()==0){
				avatarLink = "references/images/avatar.jpg"; 
			}else{
				avatarLink = temp.getLink()+"/Avatar/"+avatar.get(0);
			}
			boolean checkGetLink = false;
			String linkDownload = "";
			if(role!=null||username!=null){
				if (temp.getPrice() == 0 || appdown.checkAppOwnByUsername(username, temp.getAppId())
						|| temp.getDevId().equals(username)
						|| role.equals("Administrator")) {
					String appname = appbo.getNumberOfFile(
							getServlet().getServletContext().getRealPath("/")
									+ temp.getLink()).get(0);
					linkDownload = temp.getLink() + "/" + appname;
					checkGetLink=true;
				}
			}
			applistHTML +="<div class='image_grid portfolio_8col'>"
					+ "<ul id='list' class='portfolio_list da-thumbs'><li>"
					+ "<img src='"+avatarLink+"'>"
							+ "<article class='da-animate da-slideFromTop' "
							+ "style='display: block;'> "
							+ "<h3 class='price'>"+(temp.getPrice() > 0 ? (temp.getPrice()+"$") : "Free")+"</h3> "
							+ "<h4 class='date-upload'>"+temp.getUpVersion()+"</h4> "
							+ (!checkGetLink?"<span class='link_buy'><a id='btn-buy-"+temp.getAppId()+"'  onclick='buyItem("+temp.getAppId()+")' title='Buy'></a></span>":
								"<span class='link_buy'><a id='btn-buy-"+temp.getAppId()+"' href='"+linkDownload+"' title='Download'></a></span>")
											+ "<span class='link_post'><a  onclick='addToCart("+temp.getAppId()+")' title='Add To Cart'></a></span>"
											+ "<span class='link_see'><a href='getAppDetail.jms?appid="+temp.getAppId()+"' title='See more'></a></span>"
											+ (check?"<span class='link_share'><a onclick='deleteAppAction("+temp.getAppId()+")' title='delete'></a></span>"
											+ "<span class='link_rate'><a href='' data-toggle='modal' data-target='#myModal'  onclick='getUpdateForm("+temp.getAppId()+")'  title='update'></a></span>":"")
											+ "</article>"
											+ "<div><a class='item-g'>"+temp.getAppName()+"</a></div>"
													+ "</li></ul></div>";
		}
		}catch(DAOException e){
			e.printStackTrace();
		}
		out.print(applistHTML);
		out.close();
		return null;
	}
	
}